<?php
class DataTable{
	private $GameID;
	private $TableID;
	public $LastActive;
	public $Status;
	public $Transaction;
	public $TableRevision;
	public $NumPlayerMin;
	public $NumPlayerMax;
	public $NumPlayer;			 
	public $NumActivePlayer; 	 
	public $Players = array(); 
	public $Admin;
    public $Time;
	public $TranSactionMin;
	function __construct($gameid, $tableid){
		$this->GameID = $gameid;
		$this->TableID = $tableid;
		$this->Status = 0 ;
		$this->Transaction = 0;	
		$this->TableRevision = 0;
		$this->NumPlayerMin = 0;
		$this->NumPlayerMax = 0;
		$this->NumPlayer = 0;
		$this->NumActivePlayer = 0;
        $this->Admin           = 0 ;
        $this->Time             =0;
		$this->TranSactionMin	= -1;
	}
        function reset(){
		$this->Status = 0 ;
		$this->Transaction = 0;	
		$this->TableRevision = 0;
		$this->NumPlayerMin = 0;
		$this->NumPlayerMax = 0;
		$this->NumPlayer = 0;
		$this->NumActivePlayer = 0;
        $this->Admin           = 0 ;
        $this->Time             = 0;
		$this->TranSactionMin	= -1;
        }
	  static function swap($bytebuffer){
		$obj = new DataTable(DataUtil::readInt32( $bytebuffer ) , DataUtil::readInt32( $bytebuffer ) );
		$obj->LastActive = DataUtil::readInt32( $bytebuffer ) ;
		$obj->Status = DataUtil::readInt8( $bytebuffer ) ;
		$obj->Transaction = DataUtil::readInt32( $bytebuffer ) ;
		$obj->TableRevision = DataUtil::readInt32( $bytebuffer ) ;
		$obj->NumPlayerMin = DataUtil::readInt16( $bytebuffer ) ;
		$obj->NumPlayerMax = DataUtil::readInt16( $bytebuffer ) ;
		$obj->NumPlayer = DataUtil::readInt16( $bytebuffer ) ;
		$obj->NumActivePlayer = DataUtil::readInt16( $bytebuffer ) ;
		for($i = 0; $i < $obj->NumPlayer; $i++){
			$id = DataUtil::readInt32( $bytebuffer ) ;
			$status = DataUtil::readInt8( $bytebuffer ) ;
			$lastactive = DataUtil::readInt32( $bytebuffer ) ;
			$obj->Players[] = array("id"=>$id,"status"=>$status, "lastactive"=>$lastactive);
		}
                $obj->Admin = DataUtil::readInt32(  $bytebuffer  )  ;
                $obj->Time = DataUtil::readInt32( $bytebuffer ) ;
				$obj->TranSactionMin = DataUtil::readInt32( $bytebuffer ) ;
		return $obj;
	}
	  public function resetTransactionData(){
		if($this->Transaction > 0){
			for($i = 0; $i < $this->Transaction; $i++){
				FunctionGamePlay::deleteTransactionData($this->GameID,$this->TableID,$i);
			}
		}
		if($this->NumPlayer > 0){
			for($i = 0; $i < $this->NumPlayer; $i++){
				FunctionGamePlay::deletePlayerTransaction($this->GameID, $this->TableID, $i);
			}
		}
		FunctionGamePlay::deleteServerTransaction($this->GameID, $this->TableID);
	}
	  public function sendDataToUser($seatid, $data){
		$this->LastActive = time() ;
		$this->Transaction++;
		FunctionGamePlay::writeTransactionData($this->GameID,$this->TableID,$this->Transaction,$data);
		$seat_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID, $seatid);
		if($seat_trans == null) $seat_trans = array();
		$seat_trans[] = $this->Transaction;
		FunctionGamePlay::writePlayerTransaction($this->GameID, $this->TableID, $seatid, $seat_trans);
	}
	  public function sendDataToAll($data){
		$this->LastActive = time() ;
		$this->Transaction++;
		FunctionGamePlay::writeTransactionData($this->GameID,$this->TableID,$this->Transaction,$data);
		for($i = 0; $i < $this->NumPlayer; $i++){
			$seat_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID, $i);
			if($seat_trans == null) $seat_trans = array();
			$seat_trans[] = $this->Transaction;
			FunctionGamePlay::writePlayerTransaction($this->GameID, $this->TableID, $i, $seat_trans);
		}
	}
	  public function requestServerProcess($seatid, $data){
		$this->activePlayer($seatid);
		$size = 2 ; 
		$size += 4 ; 
		$size += count($data);
		$buf = ByteBuffer::allocate( $size ) ;
		DataUtil::writeInt16( $buf ,  $seatid ) ;
		DataUtil::writeInt32( $buf ,  count($data) ) ;
		DataUtil::writeBytes( $buf ,  $data ) ;
		$this->Transaction++;
		FunctionGamePlay::writeTransactionData($this->GameID,$this->TableID,$this->Transaction,  $buf ->_array() );
		$server_trans = FunctionGamePlay::readServerTransaction($this->GameID,$this->TableID);
		if($server_trans == null) $server_trans = array();
		$server_trans[] = $this->Transaction;
		FunctionGamePlay::writeServerTransaction($this->GameID,$this->TableID,$server_trans);
	}
	  public function postProcessedData($data){
		$this->LastActive = time() ;
		$buf = ByteBuffer::wrap( $data ) ;
		$trans_id = DataUtil::readInt32( $buf ) ;
		$nof_rev = DataUtil::readInt16( $buf ) ;
		$revs = array();
		for($i = 0; $i< $nof_rev; $i++){
			$revs[] = DataUtil::readInt16( $buf ) ;
		}
		$data_len = DataUtil::readInt32( $buf ) ;
		$data = DataUtil::readBytes( $buf , $data_len ) ;
		$this->Transaction++;
		FunctionGamePlay::writeTransactionData($this->GameID,$this->TableID,$this->Transaction);
		if($nof_rev == $this->NumPlayer){
			for($i = 0; $i< $this->NumPlayer; $i++){
				$seat_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID, $i);
				if($seat_trans == null) $seat_trans = array();
				$seat_trans[] = $this->Transaction;
				FunctionGamePlay::writePlayerTransaction($this->GameID, $this->TableID, $i, $seat_trans);
			}
		}else{
			if(is_array($revs)){
				foreach($revs as $seatid){
					$seat_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID, $seatid);
					if($seat_trans == null) $seat_trans = array();
					$seat_trans[] = $this->Transaction;
					FunctionGamePlay::writePlayerTransaction($this->GameID, $this->TableID, $seatid, $seat_trans);
				}
			}
		}
		$server_trans = FunctionGamePlay::readServerTransaction();
		if(is_array($server_trans)){
			foreach($server_trans as $key=>$tid){
				if($tid == $trans_id){
					unset($server_trans[$key]);
					$server_trans = array_values($server_trans);
					break;
				}
			}
		}
		FunctionGamePlay::deleteTransactionData($this->GameID,$this->TableID, $trans_id);
	}
	  public function removeTransactionData($mixmax){		
		if($this->TranSactionMin == - 1){
			$this->TranSactionMin = 0;
		}
			for($i = $this->TranSactionMin ; $i < $mixmax ; $i++){
				if(in_array($i , $this->getArrayIDTransactionPlayer()))
					break;
				FunctionGamePlay::deleteTransactionData($this->GameID,$this->TableID, $i);
				$this->TranSactionMin = $i;
			}				
	}
	  public function getArrayIDTransactionPlayer(){
		$_a = array();
		for($i = 0 ; $i < $this->NumPlayer ; $i++){
			$seat_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID, $i);
			if(is_array($seat_trans)){
				$_a = array_unique(array_merge($seat_trans,$_a));
			}
		}
		return $_a;
	}
	  function getNextTransaction($seatid,$lastTransaction){
		$this->activePlayer($seatid);
		$player_trans = FunctionGamePlay::readPlayerTransaction($this->GameID, $this->TableID,$seatid);
		if(is_array($player_trans)){
			if($player_trans[0] == $lastTransaction){
				unset($player_trans[0]);
				$player_trans = array_values($player_trans);
				if(count($player_trans) > 0){
					$next_trans = $player_trans[0];
					$next_trans_data = FunctionGamePlay::readTransactionData($this->GamdID, $this->TableID, $next_trans);
					if($next_trans_data == null) $next_trans_data = array();
					$size = 4 ; 
					$size += 4 ; 
					$size += count($next_trans_data); 
					$buf = ByteBuffer::allocate( $size ) ;
					DataUtil::writeInt32( $buf ,  $next_trans ) ;
					DataUtil::writeInt32( $buf ,  count($next_trans_data) ) ;
					DataUtil::writeBytes( $buf ,  $next_trans_data ) ;
					return  $buf ->_array() ;
				}else{
				}
			}else{
			}
		}
		$size = 4 ; 
		$size += 4 ; 
		$buf = ByteBuffer::allocate( $size ) ;
		DataUtil::writeInt32( $buf ,  -1 ) ;
		DataUtil::writeInt32( $buf ,  0 ) ;
		return  $buf ->_array() ;
	}
        function PostTransPlayer($gameid, $tableid,$transaction){
            $data = FunctionGamePlay::readTransactionData($gameid, $tableid, $transaction);
            return $data;
        }
	  function getNumOfPlayer(){
		return $this->NumPlayer;
	}
	  function activePlayer($seatid){
		$this->Player[$seatid]["lastactive"] = time() ;
		$this->LastActive = time() ;
	}
	  function addPlayer($playerid){
		if(is_array($this->Players)){
			foreach($this->Players as $key=>$player){
				if($player["id"] == $playerid){
					$this->activePlayer($playerid);
					$this->TableRevision = ($this->TableRevision+1)%5000;
					return $key;
				}
			}	
		}
		if($this->NumPlayer < $this->NumPlayerMax){
			$this->Players[] = array("id"=>$playerid,"status"=> 0 );
                        if($this->NumPlayer == 0)
                            $this->Admin = $playerid;  
			$this->NumPlayer++;
			$this->TableRevision = ($this->TableRevision+1)%5000;
			$this->activePlayer($this->NumPlayer-1);
			$this->NumActivePlayer++;
			return $this->NumPlayer-1;
		}
		return -1;
	}
          function removePlayer($playerid){
                $i= 0 ;
		if(is_array($this->Players)){
			foreach($this->Players as $key=>$player){
				if($player["id"] == $playerid){
                                    unset($this->Players[$key]);
                                    $this->Players = array_values($this->Players);                                    
                                    break;
				}                               
			}	
		}
                $this->Players = array_values($this->Players);
	}
	  function acceptPlayer($seatid){
		if($seatid > 0 && $seatid < $this->NumPlayer){
			$this->Players[$seatid]["status"] = 1 ;
			$this->TableRevision = ($this->TableRevision+1)%5000;
		}
	}
	  function isFull(){
		return ($this->NumPlayer == $this->NumPlayerMax);
	}
	  function canPlay(){
		if($this->NumPlayer < $this->NumPlayerMin) return false;
		for($i = 0; $i < $this->NumPlayer; $i++){
			if($this->Players[$i]["status"] == 0 )return false;
		}
		return true;
	}
	  function getBinSizeOfTableInfo(){
		$size = 0;
		$size += 4 ; 
		$size += 2 ; 
		$size += 2  * $this->NumActivePlayer;
		return $size;
	}
	  function getBinaryOfTableInfo(){
		$buf = ByteBuffer::allocate( $this->getBinSizeOfTableInfo() ) ;
		DataUtil::writeInt32( $buf ,  $this->TableRevision ) ;
		DataUtil::writeInt16( $buf ,  $this->NumActivePlayer ) ;
		if(is_array($this->Players)){
			foreach($this->Players as $key=>$player){
				if($player["status"] != 2 ){
					DataUtil::writeInt16( $buf ,  $key ) ;
				}
			}
		}
		return  $buf ->_array() ;
	}
	  function getBinSize(){
		$size = 0;
		$size += 4 ; 
		$size += 4 ; 
		$size += 4 ; 
		$size += 1 ; 
		$size += 4 ; 
		$size += 4 ; 
		$size += 2 ; 
		$size += 2 ; 
		$size += 2 ; 
		$size += 2 ; 
		$size += (4  + 1  + 4 ) * $this->NumPlayer;
                $size +=  4 ;
                $size +=  4 ;  
		$size +=  4 ;
		return $size;
	}
	  function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf ,  $this->GameID ) ;
		DataUtil::writeInt32( $buf ,  $this->TableID ) ;
		DataUtil::writeInt32( $buf ,  $this->LastActive ) ;
		DataUtil::writeInt8( $buf ,  $this->Status ) ;
		DataUtil::writeInt32( $buf ,  $this->Transaction ) ;
		DataUtil::writeInt32( $buf ,  $this->TableRevision ) ;
		DataUtil::writeInt16( $buf ,  $this->NumPlayerMin ) ;
		DataUtil::writeInt16( $buf ,  $this->NumPlayerMax ) ;
		DataUtil::writeInt16( $buf ,  $this->NumPlayer ) ;
		DataUtil::writeInt16( $buf ,  $this->NumActivePlayer ) ;
		if(is_array($this->Players)){
			foreach($this->Players as $player){
				DataUtil::writeInt32( $buf , $player["id"] ) ;
				DataUtil::writeInt8( $buf , $player["status"] ) ;
				DataUtil::writeInt32( $buf , $player["lastactive"] ) ;
			}
		}
        DataUtil::writeInt32( $buf ,  $this->Admin ) ;
        DataUtil::writeInt32( $buf ,  $this->Time ) ;
		DataUtil::writeInt32( $buf ,  $this->TranSactionMin ) ;
		return  $buf ->_array() ;
	}
}
?>